package com.dataseek.iot.sys.controller;

import com.dataseek.iot.sys.email.EmailService;
import com.dataseek.iot.sys.service.EmailServiceOld;
import com.dataseek.iot.sys.service.SysService;
import com.dataseek.iot.sys.utils.RandomCodeUtils;
import com.dataseek.iot.sys.vo.UserVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolkit.auth.entity.LoginUser;
import com.dataseek.iot.toolkit.auth.entity.SysUser;
import com.dataseek.iot.toolkit.auth.redis.config.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
public class SysController {
    @Autowired
    private SysService sysService;

    @Autowired
    private EmailServiceOld emailServiceOld;

    @Autowired
    private RedisCache redisCache;

    @RequestMapping(value = "{tenantCode}/user/login",method = RequestMethod.POST)
    public ResponseResult login(@RequestBody UserVo userVo){
        ResponseResult responseResult = sysService.login(userVo);
        return responseResult;
    }

    @RequestMapping(value = "/user/logout",method = RequestMethod.POST)
    public ResponseResult logout(){
        ResponseResult responseResult = sysService.logout();
        return responseResult;
    }

    @RequestMapping(value = "{tenantCode}/user/hello",method = RequestMethod.GET)
    public ResponseResult sayHello(@PathVariable String tenantCode){
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"鉴权后访问");
    }

    @RequestMapping(value = "/user/password/verifycode", method = RequestMethod.POST)
    public ResponseResult sendVerifyCode(String inputEmail) {
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = principal.getSysUser();
        if (inputEmail.equals(sysUser.getEmail())) {
            //发送验证码至邮箱
            Integer verifyCode = RandomCodeUtils.generateRandomCode();
            // emailServiceOld.sendVerifyCodeByMail(verifyCode, inputEmail);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("<html><head><title></title></head><body>");
            stringBuilder.append("您好<br/>");
            stringBuilder.append("您的验证码是：").append(verifyCode).append("<br/>");
            stringBuilder.append("您可以复制此验证码并返回至管理系统修改密码页面，以验证您的邮箱。<br/>");
            stringBuilder.append("此验证码只能使用一次，在");
            stringBuilder.append("15");
            stringBuilder.append("分钟内有效。验证成功则自动失效。<br/>");
            stringBuilder.append("如果您没有进行上述操作，请忽略此邮件。");
            EmailService.sendEmail(inputEmail,stringBuilder.toString(),"验证码请查收");
            //存储验证code至redis并设置过期时间(15分钟)
            redisCache.setCacheObject("code~" + sysUser.getTenantCode() + "~" + sysUser.getUserId(), verifyCode, 15, TimeUnit.MINUTES);
        } else {
            return new ResponseResult(RspStatusEnum.FAIL.getCode(), "输入的邮箱号与预留的邮箱号不一致,请检查!");
        }
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(), "发送验证码成功!");
    }
}
